package com.yld.car.market;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.common.PortTask;

public class SelectorPortActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		this.settingTitle(R.id.title, "选择港口");

		list = (ListView) findViewById(R.id.list1);
		list.setOnItemClickListener(this);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		Intent intent = getIntent();

		flag = intent.getBooleanExtra("flag", false);
		showCancelListener(" 取消选择", cancelClickListener);
		// if (flag) {
		// Button cancel = (Button) findViewById(R.id.rightButton);
		// cancel.setText("取消选择");
		// cancel.setVisibility(View.VISIBLE);
		// cancel.setOnClickListener(cancelClickListener);
		// }

		new PortTask("2", ConstantUtils.GET_CAR_ALL_PORTS_URL, list, this,
				progressBar).execute("");
	}

	private boolean flag;
	private NetworkProgressUtils utils;
	private ListView list;
	private String name;
	private ProgressBar progressBar;
	private View.OnClickListener cancelClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			mApp.setmPortInfo(null);
			SharedPreferences shared = getSharedPreferences("port_state",
					MODE_PRIVATE);

			shared.edit().putInt("port", -2).commit();
			finish();
//			new PortTask("2", ConstantUtils.GET_CAR_ALL_PORTS_URL, list,
//					SelectorPortActivity.this, progressBar).execute("");
//			
		}
	};

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {
		SharedPreferences shared = getSharedPreferences("port_state",
				MODE_PRIVATE);

		shared.edit().putInt("port", position).commit();
		String portName = adapterView.getAdapter().getItem(position).toString();

		mApp.setmPortInfo(portName);

		finish();

	}

}
